﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Service;
using LIMS.Entities;
using Castle.Core.Logging;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;

namespace LIMS.Shared
{
    public class PlantManagementPresenter : BasePresenter<IPlantManagementPageView>
    {
        private IPlantManagementPageView view;
        //private IUserManagementPageView oUserView;
        private ISiteManagementService siteService;
        private IPlantManagementService plantService;
        private IGroupManagementService groupService;
        private IDepartmentManagementService departmentService;
        private ILogger logger;
        /// <summary>
        /// Constructor of UserManagementPagePresenter
        /// </summary>
        /// <param name="view"></param>
        /// <param name="oLogger"></param>
        /// <param name="myService"></param>
        /// <param name="oGroupService"></param>
        public PlantManagementPresenter(IPlantManagementPageView view, ILogger oLogger,
             IPlantManagementService plantService,ISiteManagementService siteService,
            IGroupManagementService oGroupService, IDepartmentManagementService departmentService)
            : base(view)
        {
            
            this.view = view;
            
            this.logger = oLogger;
            this.siteService = siteService;
            this.groupService = oGroupService;
            this.plantService = plantService;
            this.departmentService = departmentService;
        }
        /// <summary>
        /// Initializing user view 
        /// </summary>
        /// <returns></returns>
        public bool InitializeView()
        {                       
            try
            {
                GridViewData temp = view.GridData;

                temp.Criteria.Clear();
                temp.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = temp;
                view.Plants = plantService.GetAllPlantForGrid(ref oGrdData);
                view.GridData = oGrdData;                
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error while initializing plants view", ex);
                return false;

            }
        }
        /// <summary>
        /// Save User
        /// </summary>
        /// <param name="newUser"></param>
        /// <returns></returns>
        public bool Save(Plant newPlant)
        {
            try
            {
                bool bIsNewUser = (newPlant.Id == 0) ? true : false;
                if (plantService.Save(newPlant))
                {
              
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error while saving plant" + newPlant.Name.ToString() + " information.", ex);
                return false;
            }
        }
        /// <summary>
        /// Delete User / Deactivate User
        /// </summary>
        /// <param name="dUser"></param>
        /// <returns></returns>
        public bool Delete(Plant dPlant)
        {
            dPlant.Status = "D";
            return plantService.Save(dPlant);
        }

        /// <summary>
        /// Edits a product, setter does the trick.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool Edit(int index)
        {
            try
            {                
                view.CurrentIndex = index;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error("Error occured while editing record. Exception : " + ex.Message, ex);
                return false;
            }
        }

        /// <summary>
        /// Search plant
        /// </summary>
        /// <param name="sUserName"></param>
        /// <returns></returns>
        public void SearchPlantByKeyword(string sKeyword)
        {
            try
            {
                view.GridData.Criteria.Clear();
                view.GridData.Criteria.Add(new GVEntityData("Name", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.Or));
                view.GridData.Criteria.Add(new GVEntityData("Location", CriteriaDataType.String, new List<string> { sKeyword }, CriteriaSearchType.Contains, CriteriaRelationWithSibling.And));
                view.GridData.Criteria.Add(new GVEntityData("Status", CriteriaDataType.String, new List<string> { Resource.ACTIVE_STATUS }, CriteriaSearchType.Equals, CriteriaRelationWithSibling.None));
                GridViewData oGrdData = view.GridData;
                List<Plant> myPlants = plantService.GetAllPlantForGrid(ref oGrdData);
                view.GridData = oGrdData;
                view.Plants = myPlants;
            }
            catch (Exception ex)
            {
                logger.Error("Error in Searching user using Keyword for :" + sKeyword, ex);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Site> GetAllSites()
        {

           
            List<Site> oSitesList = new List<Site>();
            try
            {
                oSitesList = siteService.GetAllSites();      
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting counties " + ex.ToString());
            }
            return oSitesList;

        }
        /// <summary>
        /// GetDepartmentByPlantId
        /// </summary>
        /// <param name="plantID"></param>
        /// <returns></returns>
        public bool GetDepartmentByPlantId(int plantID)
        {

            try
            {
                List<Department> oDepartment = new List<Department>();
                oDepartment = departmentService.GetAllDepartmentsByPlantId(plantID);
                if (oDepartment.Count > 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
